package com.shanghaionstar.nav;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.map.util.ToastUtil;
import com.onstar.cn.ripple.view.RippleCustomBtn;
import com.onstar.cn.weight.DeleteEditText;
import com.shanghaionstar.R;
import com.shanghaionstar.nav.iview.INavFavSaveView;
import com.shanghaionstar.nav.presenter.NavFavSavePresenter;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.MapHandler;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by 6335yz on 2016/7/18.
 */
public class NavFavSaveFragment extends BaseFragment implements INavFavSaveView {
    private PoiItemInfo poiItemInfo;

    private TopLayout favorite_destination_headview;
    @FindViewById(R.id.edit_favorite_destination)
    private DeleteEditText edit_favorite_destination;
    @FindViewById(R.id.btn_cancle)
    private RippleCustomBtn btn_cancle;
    @FindViewById(R.id.btn_submit)
    private RippleCustomBtn btn_submit;

    private ProgressDialog progressDialog;
    private AbstractMapActivity mapActivity;
    private NavFavSavePresenter navFavSavePresenter;
    private MapHandler mapHandler;
    private FragmentManager fragmentManager;


    @FindViewById
    private TopLayout top_layout;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            poiItemInfo = (PoiItemInfo) getArguments().getSerializable(Constants.POIITEMINFO);
            navFavSavePresenter = new NavFavSavePresenter(mapActivity.getScheduler(), mapActivity, mapHandler);
            navFavSavePresenter.setView(this);
            initView();
        }
        top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
    }

    public void initView() {
        edit_favorite_destination.setText(poiItemInfo.getTitle());

    }

    @Override
    public int getLayoutId() {
        return R.layout.nav_poi_detail_favotite_destination;
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mapActivity = (AbstractMapActivity) activity;
        mapHandler = mapActivity.getMapHandler();
        fragmentManager = mapActivity.getSupportFragmentManager();
    }

    @OnClick({R.id.btn_cancle, R.id.btn_submit})
    public void OnClick(View view) {
        mapActivity.hideSoftInput();
        switch (view.getId()) {
            case R.id.btn_cancle:
                mapActivity.removeCurrentAndShowPrevFragment(fragmentManager, getClass());
                break;
            case R.id.btn_submit:
                poiItemInfo.setNickName(edit_favorite_destination.getText().toString().trim());
                navFavSavePresenter.addFavoriteDestination(String.valueOf(edit_favorite_destination.getText()), poiItemInfo);
                break;
        }
    }

    @Override
    public void addFavoriteDestination(String name, PoiItemInfo poiItemInfo) {

    }

    @Override
    public void setLoading(boolean isLoading) {
        if (isLoading) {
            progressDialog = showLoadProgress();
            progressDialog.show();
        } else {
            dismissDialog(progressDialog);
        }
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showAlertDialog(mapActivity, getString(R.string.dialog_warning),
                e.getMessage());

    }

    @Override
    public void onReponseSuccess(Object object) {
        fragmentManager.popBackStack(getClass().getSimpleName(), 0);
    }

    @Override
    public Activity onActivity() {
        return null;
    }

    public static NavFavSaveFragment newInstance(PoiItemInfo poiItemInfo) {
        NavFavSaveFragment navFavSaveFragment = new NavFavSaveFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("poiItemInfo", poiItemInfo);
        navFavSaveFragment.setArguments(bundle);
        return navFavSaveFragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void showToast(String msg) {
        ToastUtil.show(mapActivity, msg);
    }

    @Override
    public void closePage() {
        goBack();
    }
}
